import 'package:flutter/material.dart';
import 'package:hxdj/common/my_common.dart';
import 'package:hxdj/common/my_constant.dart';
import 'package:hxdj/login/user_model.dart';
import 'dart:convert';

import 'package:provider/provider.dart';

// 全局配置
class MyGlobalViewModel extends ChangeNotifier {
  // 全局配置
  bool isLogin = false;

  // 是否同意用户协议
  bool isAgreeUserAgreement = false;

  late UserModel? userModel = UserModel();

  //用户的详细信息
  late UserDetailModel? userDetailModel = UserDetailModel();

  //公共数据
  late StandardDetailModel? standardDetailModel = StandardDetailModel();

  int index = 0;

  static MyGlobalViewModel getGlobalViewModel(BuildContext context) {
    MyGlobalViewModel globalViewModel = Provider.of<MyGlobalViewModel>(context, listen: false);
    return globalViewModel;
  }

  MyGlobalViewModel() {
    isAgreeUserAgreement = CommonPlugin.sharedPreferences?.getBool(MyConstant.isAgreeUserAgreement) ?? false;

    isLogin = CommonPlugin.sharedPreferences?.getBool(MyConstant.isLogin) ?? false;

    //获取用户信息
    String? userInfoStr = CommonPlugin.sharedPreferences!.getString(MyConstant.userInfo) ?? "";
    if (CommonPlugin.isEmpty(userInfoStr)) {
      return;
    }
    userModel = UserModel.fromJson(userInfoStr);
    notifyListeners();
  }

  void refresh() {
    isLogin = CommonPlugin.sharedPreferences?.getBool(MyConstant.isLogin) ?? false;
    notifyListeners();
  }

  //修改tabbr的索引值
  setIndex(x) {
    index = x;
    notifyListeners();
  }

  //更新用户信息
  updateUserInfo(UserDetailModel? userDetailModel) {
    this.userDetailModel = userDetailModel;
    notifyListeners();
  }

  @override
  void dispose() {
    super.dispose();
    debugPrint("MyGlobalViewModel销毁了");
  }
}

class StandardDetailModel {
  final String? gonganRecords;
  final String? icpRecords;
  final String? complaintNotice;
  final int? complaintTextNums;
  final List<String>? complaintList;
  final int? noticeNums;
  final String? banbs;
  final String? kefuMobile;
  final String? kefuQrcode;
  final String? tousuQrcode;
  final String? h5Url;

  StandardDetailModel({
    this.gonganRecords,
    this.icpRecords,
    this.complaintNotice,
    this.complaintTextNums,
    this.complaintList,
    this.noticeNums,
    this.banbs,
    this.kefuMobile,
    this.kefuQrcode,
    this.tousuQrcode,
    this.h5Url,
  });

  factory StandardDetailModel.fromJson(String str) => StandardDetailModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory StandardDetailModel.fromMap(Map<String, dynamic> json) => StandardDetailModel(
        gonganRecords: json["gongan_records"],
        icpRecords: json["icp_records"],
        complaintNotice: json["complaint_notice"],
        complaintTextNums: json["complaint_text_nums"],
        complaintList: json["complaint_list"] == null ? [] : List<String>.from(json["complaint_list"]!.map((x) => x)),
        noticeNums: json["notice_nums"],
        banbs: json["banbs"],
        kefuMobile: json["kefu_mobile"],
        kefuQrcode: json["kefu_qrcode"],
        tousuQrcode: json["tousu_qrcode"],
        h5Url: json["h5_url"],
      );

  Map<String, dynamic> toMap() => {
        "gongan_records": gonganRecords,
        "icp_records": icpRecords,
        "complaint_notice": complaintNotice,
        "complaint_text_nums": complaintTextNums,
        "complaint_list": complaintList == null ? [] : List<dynamic>.from(complaintList!.map((x) => x)),
        "notice_nums": noticeNums,
        "banbs": banbs,
        "kefu_mobile": kefuMobile,
        "kefu_qrcode": kefuQrcode,
        "tousu_qrcode": tousuQrcode,
        "h5_url": h5Url,
      };
}
